package com.kjc.demo.rcservice.util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;

public class ApkSignUtil {
	
	@SuppressWarnings("resource")
	public static String getApkVersion(File file){
		ApkFile apkFile = null;
		ApkMeta apkMeta = null;
		try {
			apkFile = new ApkFile(file);
			apkMeta = apkFile.getApkMeta();
		} catch (IOException e) {
			return "";
		}
		
		return apkMeta.getVersionName();
	}
	
	@SuppressWarnings("resource")
	@Deprecated
	public static String getApkVersion(MultipartFile file){
		File newFile = null;
		try {
			FileUtils.copyInputStreamToFile(file.getInputStream(),newFile);
		} catch (IOException e1) {
			return "";
		}
		ApkFile apkFile = null;
		ApkMeta apkMeta = null;
		try {
			apkFile = new ApkFile(newFile);
			apkMeta = apkFile.getApkMeta();
		} catch (IOException e) {
			return "";
		}
		
		return apkMeta.getVersionName();
	}
	
	@SuppressWarnings("resource")
	public static String getApkVersion(String filePath){
		ApkFile apkFile = null;
		ApkMeta apkMeta = null;
		try {
			apkFile = new ApkFile(new File(filePath));
			apkMeta = apkFile.getApkMeta();
		} catch (IOException e) {
			return "";
		}
		
		return apkMeta.getVersionName();
	}
	
//	public static void main(String[] args) {
//		try {
//			ApkFile apkFile = new ApkFile(new File("F:\\upload\\app-debug(1).apk"));
//			ApkMeta apkMeta = apkFile.getApkMeta();
//			String v1 = getApkVersion("F:\\upload\\app-debug(1).apk");
//			File file = new File("F:\\upload\\app-debug(1).apk");
//			String v2 = getApkVersion(file);
//			System.out.println("v1="+v1+"--v2="+v2);
//			System.out.println(apkMeta.getLabel());
//			System.out.println(apkMeta.getPackageName());
//			System.out.println(apkMeta.getVersionCode());
//			System.out.println(apkMeta.getVersionName());
//			/*for (UseFeature feature : apkMeta.getUsesFeatures()) {
//				System.out.println(feature.getName());
//			}*/
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}
